package edu.uic.kott.activities;

import edu.uic.keyoutcomestrackertool.R;
import edu.uic.kott.fragments.TimePickerFragment;

import android.app.AlertDialog;
import android.app.DialogFragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TimePicker;
import android.widget.Toast;

public class DayTime extends FragmentActivity implements TimePickerFragment.TimePickerDialogListener{

	private EditText startTimeEditText;
	private EditText endTimeEditText;
	private static final int START_TIME_PICKER_ID = 1;
	private static final int END_TIME_PICKER_ID = 2;
	private static final String LOGTAG = "KOTT";
	private CheckBox mondayCheckBox;
	private CheckBox tuesdayCheckBox;
	private CheckBox wednesdayCheckBox;
	private CheckBox thursdayCheckBox;
	private CheckBox fridayCheckBox;
	private CheckBox saturdayCheckBox;
	private CheckBox sundayCheckBox;

	private String sTime;
	private String eTime;
	private boolean correctTime;
	private String days;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.day_time);

		mondayCheckBox = (CheckBox) findViewById(R.id.mondayCheckBox);
		tuesdayCheckBox = (CheckBox) findViewById(R.id.tuesdayCheckBox);
		wednesdayCheckBox = (CheckBox) findViewById(R.id.wednesdayCheckBox);
		thursdayCheckBox = (CheckBox) findViewById(R.id.thursdayCheckBox);
		fridayCheckBox = (CheckBox) findViewById(R.id.fridayCheckBox);
		saturdayCheckBox = (CheckBox) findViewById(R.id.saturdayCheckBox);
		sundayCheckBox = (CheckBox) findViewById(R.id.sundayCheckBox);

		startTimeEditText = (EditText) findViewById(R.id.startTimeEditText);
		endTimeEditText = (EditText) findViewById(R.id.endTimeEditText);

		sTime = "";
		eTime = "";
		correctTime = false;
		days = "";
	}

	public void InputStartTime(View v) {
		// supply the appropriate id - I'm assuming you'll be adding an InputEndTime method somewhere that will then supply END_TIME_PICKER_ID 
		DialogFragment newFragment = TimePickerFragment.newInstance(START_TIME_PICKER_ID);
		newFragment.show(getFragmentManager(), "timePicker");
	}

	public void InputEndTime(View v) {
		// supply the appropriate id - I'm assuming you'll be adding an InputEndTime method somewhere that will then supply END_TIME_PICKER_ID 
		DialogFragment newFragment = TimePickerFragment.newInstance(END_TIME_PICKER_ID);
		newFragment.show(getFragmentManager(), "timePicker");
	}

	@Override public void onTimeSet(int id, TimePicker view, int hourOfDay, int minute) {
		switch (id) {
		case 1:
			startTimeEditText.setText(String.valueOf(hourOfDay)+":"+String.valueOf(minute));
			break;

		case 2:
			endTimeEditText.setText(String.valueOf(hourOfDay)+":"+String.valueOf(minute));
			break;		
		}
	}

	private void compareTime() {
		if((startTimeEditText.length() > 0) && (endTimeEditText.length() > 0)){

			Toast msg=null;
			sTime = startTimeEditText.getText().toString();
			Log.i(LOGTAG,sTime);
			eTime = endTimeEditText.getText().toString();
			Log.i(LOGTAG,sTime);
			if(((sTime.compareTo(eTime)) >= 0)){
				correctTime = false;
				msg = Toast.makeText(this, "INCORRECT", Toast.LENGTH_SHORT);
			}
			else{
				correctTime = true;
				msg = Toast.makeText(this, "CORRECT", Toast.LENGTH_SHORT);
			}
			msg.show();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.menu_save, menu);

		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		int daysSelected = 0;
		days ="";

		if(mondayCheckBox.isChecked()){
			days = days + "Monday ";
			daysSelected++;
		}
		if(tuesdayCheckBox.isChecked()){
			days = days + "Tuesday ";
			daysSelected++;
		}
		if(wednesdayCheckBox.isChecked()){
			days = days + "Wednesday ";
			daysSelected++;
		}
		if(thursdayCheckBox.isChecked()){
			days = days + "Thursday ";
			daysSelected++;
		}
		if(fridayCheckBox.isChecked()){
			days = days + "Friday ";
			daysSelected++;
		}
		if(saturdayCheckBox.isChecked()){
			days = days + "Saturday ";
			daysSelected++;
		}
		if(sundayCheckBox.isChecked()){
			days = days + "Sunday ";
			daysSelected++;
		}

		compareTime();

		if(correctTime && daysSelected > 0){
			Intent intent = new Intent(this, AddEditCourse.class);
			intent.putExtra("days", days);
			intent.putExtra("startTime", sTime);
			intent.putExtra("endTime", eTime);
			intent.putExtra("screen", "classSchedule");				//to determine the screen flow
			setResult(RESULT_OK, intent);
			finish();
		}
		else if(daysSelected == 0) {
			invalidScheduleDialog();
		}
		else if(!correctTime){
			invalidTimeDialog();
		}
		return true;		
	}

	public void invalidScheduleDialog(){
		AlertDialog.Builder builder = new AlertDialog.Builder(DayTime.this);
		builder.setTitle(R.string.dialogTitle_invalidClassSchedule);	
		builder.setMessage(R.string.dialogMsg_invalidClassSchedule);
		builder.setPositiveButton("OK", null);

		AlertDialog errorDialog = builder.create();
		errorDialog.show();		
	}

	public void invalidTimeDialog(){
		AlertDialog.Builder builder = new AlertDialog.Builder(DayTime.this);
		builder.setTitle(R.string.dialogTitle_invalidTime);	
		builder.setMessage(R.string.dialogMsg_invalidTime);
		builder.setPositiveButton("OK", null);

		AlertDialog errorDialog = builder.create();
		errorDialog.show();
	}
}
